home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-PR / MPW / Scripts / CompareFiles < prev    next >
Encoding:
Text File  |  1998-04-06  |  12.4 KB  |  374 lines  |  [TEXT/MPS ]

  1. # CompareFiles - Compare two source files and show the differences
  2. #
  3. # Version 3.4.2d1
  4. #
  5. # Usage:
  6. #    CompareFiles [-TopDown] [-compareOpts options…] [-9 | -LC | -12 | -13 | -Portrait | -15 | -16 | -17 | -TwoPage | -21 | -b x y | -r t,l,b,r] oldFile (newFile | newDir)
  7. #        -9                    use 9 inch screen (512 x 342)
  8. #        -LC    | -12            use 12 inch screen (512 x 384)
  9. #        -13                    use 13 inch screen (640 x 480)
  10. #        -Portrait | -15        use 15 inch screen (640 x 870)
  11. #        -16                    use 16 inch screen (832 x 624)
  12. #        -17                    use 17 inch screen Multiscan at max scan (1024 x 768)
  13. #        -TwoPage | -21        use 21 inch screen (1152 x 870)
  14. #        -b x y                use screen dimensions given in x y
  15. #        -r t,l,b,r            use screen rect given by Top,Left,Bottom,Right
  16. #        -TopDown            allow editing from top to bottom rather than bottom to top.
  17. #        -compareOpts        additional options passed along to the Compare tool
  18. #
  19. #            The default is to use the dimensions of the main monitor.
  20. #
  21. #        NOTE:  
  22. #            No error checking is done on x y coordinates
  23. #
  24. # Returns:
  25. #    Status = 0 - files match
  26. #    Status = 1 - syntax error
  27. #    Status = 2 - files differ  (this is the same status as the Compare tool)
  28. #
  29. # CompareFiles will compare two files, and if they don't match it will bring them
  30. # both up on the screen and put them side by side.  A menu will be appended to the
  31. # menu bar to go through the changes.  When all the changes have been shown, the
  32. # file windows will be restored and closed.
  33. #
  34. # In addition to the menu the following keys are provided to do the menu operations:
  35. #
  36. #            Control+UpArrow         Moves to the next  mismatch
  37. #            Control+DownArrow         Moves to the next  mismatch (same as Control+UpArrow)
  38. #            Control+RightArrow         Copy selection from left file to right file
  39. #            Control+LeftArrow         Copy selection from right file to left file
  40. #
  41. # If the -TopDown option is not specified the differences are shown from bottom to top
  42. # so that if any changes are made in the files the line offsets are not messed up.  If
  43. # -TopDown is specified, the changes CAN be made from the top down due to the fact that
  44. # the files are recompared before moving to the next mismatch.  Although this may sound
  45. # more inefficient than bottom-up comparing, it is in fact faster!  This is because the
  46. # Compare tool is very fast plus it only has to read the files starting at the previous
  47. # mismatch and quits as soon as the next mismatch is found.  This is the more natural
  48. # way to edit the files.  Bottom-up processing is supported (as the default) only for
  49. # compatibility.  It's slower since a helper script must be executed to search a file
  50. # of Compare output for the previous mismatch.
  51. #
  52. # When not using -TopDown, the temporary file {TempFolder}Compare_File is created to hold
  53. # the Compare output.  This file is deleted on exit.
  54. #
  55. # Copyright Apple Computer, Inc. 1988 - 1998
  56. # All rights reserved.
  57. #
  58. # Version 3.3 by Peter Potrebic with enhancements by Greg Branche
  59. # Version 3.4.1 enhancements by Ira L. Ruben
  60. # Version 3.4.2 enhancements by Greg Branche
  61. #
  62. # Helper script and tools referenced by this script:
  63. #
  64. #    CompareFiles•NextChange - script to show next change (used when not using -Topdown)
  65. #    CompareFiles•Quit        - script to clean up when finished
  66. #    Monitors                - command to get default main screen dimensions
  67. #
  68. # Implementation details -- The -TopDown option assumes the new version of Compare which
  69. # supports the -selection option for getting and setting selections.
  70. #
  71. # The Monitors command is an MPW command that is used by this script to get the dimensions
  72. # of the main monitor as the default display size.
  73. #
  74.  
  75. Set Exit 0
  76. Set CaseSensitive 0
  77. Set ErrorMessage     "### CompareFiles - Incorrect order, or missing or illegal option"
  78. Set Usage            "# Usage - CompareFiles [-TopDown] [-compareOpts options...] [-9 | -LC | -12 | -13 | -Portrait | -15 | -16 | -17 | -TwoPage | -21 | -b x y | -r t,l,b,r] oldFile (newFile | newDir)"
  79.  
  80. Set CompareOptions "-g 3 -b -m -t {DefaultCompareFilesOptions}"
  81. UnSet File1
  82. UnSet File2
  83.  
  84. (Evaluate "`Monitors`" =~ /(≈)®1,(≈)®2,(≈)®3,(≈)®4/) >Dev:Null
  85. Set Top     "{®1}"
  86. Set Left     "{®2}"
  87. Set Bottom     "{®3}"
  88. Set Right     "{®4}"
  89. Evaluate Left   = {Left} + 5
  90. Evaluate Right  = {Right} - 4
  91. Evaluate Bottom = {Bottom} - 142
  92. Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  93. Evaluate Bottom = {Bottom} + 1
  94. Evaluate Right  = {Right} - 9
  95.  
  96. Set TopDown 0
  97.  
  98. loop # process the options...
  99.     Break if {#} == 0
  100.     If !{#}                                                # Done?
  101.         If !"{File2}"
  102.             Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  103.             Exit 1
  104.         End
  105.         Break
  106.     End
  107.     
  108.     If "{1}" !~ /-≈/                                    # Filename?
  109.         If !"{File1}"
  110.             Set File1 "{1}"
  111.             Shift
  112.             Continue
  113.         end
  114.         If !"{File2}"
  115.             Set File2 "{1}"
  116.             Shift
  117.             Continue
  118.         end
  119.         Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  120.         Exit 1
  121.     End
  122.     If "{1}" =~ /-td/    || "{1}" =~ /-topdown/            # -topdown
  123.         Set TopDown 1
  124.         Shift 1
  125.         Continue
  126.     End
  127.     If "{1}" =~ /-TwoPage/ || "{1}" =~ /-21/            # -TwoPage, -21
  128.         Set CompareFilesTileRectangle "0, 5, 727, 1148"
  129.         Set Bottom 728
  130.         Set Right 1139
  131.         Shift 1
  132.         Continue
  133.     End
  134.     If "{1}" =~ /-17/                                    # -17
  135.         Set CompareFilesTileRectangle "0, 5, 625, 1020"
  136.         Set Bottom 626
  137.         Set Right  1011
  138.         Shift 1
  139.         Continue
  140.     End
  141.     If "{1}" =~ /-Portrait/ || "{1}" =~ /-15/            # -Portrait, -15
  142.         Set CompareFilesTileRectangle "0, 5, 754, 635"
  143.         Set Bottom 755
  144.         Set Right  626
  145.         Shift 1
  146.         Continue
  147.     End
  148.     If "{1}" == "-16"                                    # -16 (Apple 16" screen)
  149.         Set CompareFilesTileRectangle "0, 5, 482, 828"
  150.         Set Bottom 483
  151.         Set Right  819
  152.         Shift 1
  153.         Continue
  154.     End
  155.     If "{1}" =~ /-13/                                    # -13 (MacII screen)
  156.         Set CompareFilesTileRectangle "0, 5, 344, 635"
  157.         Set Bottom 345
  158.         Set Right  626
  159.         Shift 1
  160.         Continue
  161.     End
  162.     If "{1}" =~ /-LC/ || "{1}" =~ /-12/                    # -LC (LC 12" RGB screen)
  163.         Set CompareFilesTileRectangle "0, 3, 240, 510"
  164.         Set Bottom 241
  165.         Set Right  500
  166.         Shift 1
  167.         Continue
  168.     End
  169.     If "{1}" == "-9"                                    # -9 (Mac+ and MacSE screen)
  170.         Set CompareFilesTileRectangle "0, 3, 198, 510"
  171.         Set Bottom 199
  172.         Set Right  500
  173.         Shift 1
  174.         Continue
  175.     End
  176.     If "{1}" == "-b" || "{1}" == "-B"                    # -b x y
  177.         Evaluate Right = {2} - 5
  178.         Evaluate Bottom = {3} - 140
  179.         Set CompareFilesTileRectangle "0,5,{Bottom},{Right}"
  180.         Evaluate Bottom = {Bottom} + 1
  181.         Evaluate Right = {Right} - 9
  182.         Shift 3
  183.         Continue
  184.     End
  185.     If "{1}" == "-r" || "{1}" == "-R"                    # -r t,l,b,r
  186.         (Evaluate "{2}" =~ /(≈)®1,(≈)®2,(≈)®3,(≈)®4/) >Dev:Null
  187.         Set Top "{®1}"
  188.         Set Left "{®2}"
  189.         Set Bottom "{®3}"
  190.         Set Right "{®4}"
  191.         Evaluate Bottom = {Bottom} - 120
  192.         Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  193.         Evaluate Bottom = {Bottom} + 1
  194.         Evaluate Left = {Left} + 2
  195.         Evaluate Right = {Right} - {Left} - 2
  196.         Shift 2
  197.         Continue
  198.     end
  199.     If "{1}" == "-compareOpts"
  200.         Set CompareOptions "{CompareOptions} {2}"
  201.         Shift 2
  202.         Continue
  203.     end
  204.         Alert "Invalid option ("{1}")∂n∂n{Usage}"
  205.         exit 1
  206. end
  207.  
  208. if !"{File1}" || !"{File2}"
  209.     Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  210.     Exit 1
  211. end
  212.  
  213. # add the ability to specify a filename and a directory
  214. # the same named file from the two directories is then compared
  215.  
  216. set File1Directory "`Exists -d "{File1}"`" 
  217. if ("{File1Directory}"  != "")
  218.     set File1 "{File1Directory}{File2}"
  219. end
  220. set File2Directory "`exists -d "{File2}"`" 
  221. if ("{File2Directory}"  != "")
  222.     set File2 "{File2Directory}{File1}"
  223. end
  224.  
  225. if  ("{File1Directory}"  != "") && ("{File2Directory}"  != "")
  226.     Alert "Must specify at least one file and one directory file.∂n∂n{Usage}"
  227.     exit 1
  228. end
  229.     
  230.  
  231. if {TopDown}
  232.     Evaluate Bottom = {Bottom} + 119
  233.     Evaluate Right  = {Right}  + 9
  234.     Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  235.     Evaluate Bottom = {Bottom} + 1
  236.     Evaluate Right  = {Right}  - 9
  237. end
  238.  
  239. Set ActiveAtStart "{Active}"
  240. Set TargetAtStart "{Target}"
  241.  
  242. Set CompareStatus 0
  243. Compare {CompareOptions} "{{File1}}" "{{File2}}" > "{TempFolder}"Compare_File || Set CompareStatus {Status}
  244.  
  245. if {CompareStatus} == 0
  246.     Echo "### files are equal (ignoring blanks): {File1} - {File2}"
  247.     Delete -i "{TempFolder}Compare_File" ≥ dev:null
  248. else if {CompareStatus} == 2 || {CompareStatus} == 4    # files don’t match - set everything up
  249.     if !{TopDown}
  250.     StreamEdit "{{TempFolder}}"Compare_File -o "{{TempFolder}}"Compare_File ∂
  251.         -e '/•[ ∂t]*File (≈)®1; Line (≈)®2; File (≈)®3; Line (≈)®4/ Change "    Find "®2" "®1"; Find "®4" "®3'
  252.     Set NothingMatches 1
  253.     Search -s /•∂*∂*∂* Nothing seems to match ∂*∂*∂*/ "{{TempFolder}}"Compare_File ∑∑dev:null || Set NothingMatches 0
  254.     else if {CompareStatus} == 4
  255.         Set NothingMatches 1
  256.     else
  257.         Set NothingMatches 0
  258.     end
  259.     If {NothingMatches}
  260.         Beep
  261.         Confirm "Nothing matches!  Continue the file comparison?"
  262.         If {Status} == 4
  263.             if !{TopDown}
  264.                 Delete -i "{{TempFolder}}"Compare_File
  265.             end
  266.             Exit 3
  267.         End
  268.     End
  269.  
  270.     Set File1 "`Files -f -q "{File1}"`"            # get full pathnames
  271.     Set File2 "`Files -f -q "{File2}"`"
  272.  
  273.     Unset Close1 Close2                 # remember if either file is already open
  274.     Set openWindows " ``Windows -q`` "
  275.     If "{openWindows}" !~ /≈ [∂']*"{File1}"[∂']* ≈/; Open "{File1}"; Set Close1 1; End
  276.     If "{openWindows}" !~ /≈ [∂']*"{File2}"[∂']* ≈/; Open "{File2}"; Set Close2 1; End
  277.     
  278.     # Save current selection so we can restore it when we're done
  279.     Mark -y § __cfSelectionOnEntry__ "{{File1}}" ≥dev:null
  280.     Mark -y § __cfSelectionOnEntry__ "{{File2}}" ≥dev:null
  281.     
  282.     set W1location "`MoveWindow "{{File1}}";echo ∂;;SizeWindow "{{File1}}"`"
  283.     set W2location "`MoveWindow "{{File2}}";echo ∂;;SizeWindow "{{File2}}"`"
  284.     
  285.     If "``SetKey Control-RightArrow ≥dev:null``" == ""
  286.         Set ResetRightArrow "UnsetKey Control-RightArrow"
  287.     Else
  288.         Set ResetRightArrow "``SetKey Control-RightArrow ≥dev:null``"
  289.     End; ∂
  290.     If "``SetKey Control-LeftArrow ≥dev:null``" == ""
  291.         Set ResetLeftArrow "UnsetKey Control-LeftArrow"
  292.     Else
  293.         Set ResetLeftArrow "``SetKey Control-LeftArrow ≥dev:null``"
  294.     End; ∂
  295.     If "``SetKey Control-DownArrow ≥dev:null``" == ""
  296.         Set ResetDownArrow "UnsetKey Control-DownArrow"
  297.     Else
  298.         Set ResetDownArrow "``SetKey Control-DownArrow ≥dev:null``"
  299.     End; ∂
  300.     If "``SetKey Control-UpArrow ≥dev:null``" == ""
  301.         Set ResetUpArrow "UnsetKey Control-UpArrow"
  302.     Else
  303.         Set ResetUpArrow "``SetKey Control-UpArrow ≥dev:null``"
  304.     End; ∂
  305.  
  306.     # set up clean-up script
  307.     Begin 
  308.         Echo Begin
  309.         Echo ∂tSet Exit 0
  310.         Echo ∂tDeleteMenu Compare
  311.         Echo ∂t"{ResetRightArrow}"
  312.         Echo ∂t"{ResetLeftArrow}"
  313.         Echo ∂t"{ResetDownArrow}"
  314.         Echo ∂t"{ResetUpArrow}"
  315.         if !{TopDown}        
  316.             Echo -n ∂t;Quote Close -y "{{TempFolder}}Compare_File"
  317.             Echo -n ∂t;Quote Delete -i "{{TempFolder}}Compare_File"
  318.         end
  319.         Echo ∂t"{W1location}"
  320.         Echo -n ∂t;Quote Find __cfSelectionOnEntry__ "{{File1}}"
  321.         Echo -n ∂t;Quote Unmark __cfSelectionOnEntry__ "{{File1}}" ≥dev:null
  322.         If {Close1}
  323.             Echo -n ∂t;Quote Close "{{File1}}"
  324.         End
  325.         Echo ∂t"{W2location}"
  326.         Echo -n ∂t;Quote Find __cfSelectionOnEntry__ "{{File2}}"
  327.         Echo -n ∂t;Quote Unmark __cfSelectionOnEntry__ "{{File2}}" ≥dev:null
  328.         If {Close2}
  329.             Echo -n ∂t;Quote Close "{{File2}}"
  330.         End
  331.         Echo -n ∂t;Quote Open "{ActiveAtStart}"
  332.         If "{TargetAtStart}" ;∂
  333.             Echo -n ∂t;Quote Open -t "{TargetAtStart}"
  334.         End
  335.         Echo End ∂∑ Dev:Null;
  336.     End > "{{TempFolder}}Compare_State"
  337.  
  338.     TileWindows -v -r "{CompareFilesTileRectangle}" "{{File1}}" "{{File2}}"
  339.     
  340.     if !{TopDown}
  341.         Open -t "{{TempFolder}}"Compare_File
  342.         MoveWindow {Left} {Bottom} "{TempFolder}"Compare_File
  343.         SizeWindow {Right} 75 "{TempFolder}"Compare_File
  344.         Find ∞ "{{TempFolder}}"Compare_File
  345.     end
  346.  
  347.     if !{TopDown}
  348.         AddMenu Compare "Find Next Change/ƒ"    "CompareFiles•NextChange"
  349.         SetKey  Control-DownArrow                "CompareFiles•NextChange"
  350.         SetKey  Control-UpArrow                    "CompareFiles•NextChange"
  351.     else
  352.         AddMenu Compare 'Compare Again'            'Compare '"{CompareOptions}"' -selection set  "'"{{File1}}"'" "'"{{File2}}"'" && CompareFiles•Quit "The files match"'
  353.         AddMenu Compare    "(-0"                    ""
  354.         AddMenu Compare 'Find Next Change/ƒ'    'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && CompareFiles•Quit "The files match"'
  355.         SetKey  Control-DownArrow                'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && CompareFiles•Quit "The files match"'
  356.         SetKey  Control-UpArrow                    'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && CompareFiles•Quit "The files match"'
  357.     end
  358.     AddMenu    Compare "(-1" ""
  359.     AddMenu Compare "Copy Selection »»"            "Catenate ∂"{{File1}}∂".§ > ∂"{{File2}}∂".§"
  360.     SetKey  Control-RightArrow                    "Catenate ∂"{{File1}}∂".§ > ∂"{{File2}}∂".§"
  361.     AddMenu Compare "«« Copy Selection"            "Catenate ∂"{{File2}}∂".§ > ∂"{{File1}}∂".§"
  362.     SetKey  Control-LeftArrow                    "Catenate ∂"{{File2}}∂".§ > ∂"{{File1}}∂".§"
  363.     AddMenu    Compare "(-2"                         ""
  364.     AddMenu    Compare "Done"                        "CompareFiles•Quit"
  365.  
  366.     if !{TopDown}
  367.         CompareFiles•NextChange
  368.     else
  369.         Compare {CompareOptions} -selection set "{{File1}}" "{{File2}}" && CompareFiles•Quit "The files match"
  370.     end
  371. end
  372.  
  373. Exit {CompareStatus}
  374.